package vip.xiaonuo.supplier.modular.base.carrier.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.supplier.modular.base.carrier.entity.SupplierCarrier;
import vip.xiaonuo.supplier.modular.base.carrier.mapper.SupplierCarrierMapper;
import vip.xiaonuo.supplier.modular.base.carrier.param.SupplierCarrierAddParam;
import vip.xiaonuo.supplier.modular.base.carrier.param.SupplierCarrierEditParam;
import vip.xiaonuo.supplier.modular.base.carrier.param.SupplierCarrierIdParam;
import vip.xiaonuo.supplier.modular.base.carrier.param.SupplierCarrierPageParam;
import vip.xiaonuo.supplier.modular.base.carrier.service.SupplierCarrierService;

import java.util.List;

/**
 * 承运人Service接口实现类
 *
 * @author Mr.wu
 * @date  2025/03/15 19:09
 **/
@Service
public class SupplierCarrierServiceImpl extends ServiceImpl<SupplierCarrierMapper, SupplierCarrier> implements SupplierCarrierService {

    @Override
    public Page<SupplierCarrier> page(SupplierCarrierPageParam supplierCarrierPageParam) {
        QueryWrapper<SupplierCarrier> queryWrapper = new QueryWrapper<SupplierCarrier>().checkSqlInjection();
        if(ObjectUtil.isNotEmpty(supplierCarrierPageParam.getCarrierName())) {
            queryWrapper.lambda().like(SupplierCarrier::getCarrierName, supplierCarrierPageParam.getCarrierName());
        }
        //数据归属
        queryWrapper.lambda().eq(SupplierCarrier::getCompanyId, StpLoginUserUtil.getLoginUser().getCompanyId());
        if(ObjectUtil.isAllNotEmpty(supplierCarrierPageParam.getSortField(), supplierCarrierPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(supplierCarrierPageParam.getSortOrder());
            queryWrapper.orderBy(true, supplierCarrierPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(supplierCarrierPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(SupplierCarrier::getId);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(SupplierCarrierAddParam supplierCarrierAddParam) {
        SupplierCarrier supplierCarrier = BeanUtil.toBean(supplierCarrierAddParam, SupplierCarrier.class);
        //数据归属
        supplierCarrier.setCompanyId(StpLoginUserUtil.getLoginUser().getCompanyId());
        this.save(supplierCarrier);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SupplierCarrierEditParam supplierCarrierEditParam) {
        SupplierCarrier supplierCarrier = this.queryEntity(supplierCarrierEditParam.getId());
        BeanUtil.copyProperties(supplierCarrierEditParam, supplierCarrier);
        this.updateById(supplierCarrier);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<SupplierCarrierIdParam> supplierCarrierIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(supplierCarrierIdParamList, SupplierCarrierIdParam::getId));
    }

    @Override
    public SupplierCarrier detail(SupplierCarrierIdParam supplierCarrierIdParam) {
        return this.queryEntity(supplierCarrierIdParam.getId());
    }

    @Override
    public SupplierCarrier queryEntity(String id) {
        SupplierCarrier supplierCarrier = this.getById(id);
        if(ObjectUtil.isEmpty(supplierCarrier)) {
            throw new CommonException("承运人不存在，id值为：{}", id);
        }
        return supplierCarrier;
    }
}
